package com.walker.taogong.bll.impl;

import java.util.List;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.bll.UserBll;
import com.walker.taogong.dal.TaskDal;
import com.walker.taogong.dal.UserDal;
import com.walker.taogong.datamodel.Task;
import com.walker.taogong.datamodel.User;
import com.walker.taogong.datamodel.Worker;

public class UserBllImpl implements UserBll
{

    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(UserBllImpl.class);
    
    
    /************************ DAL 服务引用 *******************************************/
    
    private UserDal userDal;
    
    public void setUserDal(UserDal userDal)
    {
        this.userDal = userDal;
    }
    
    private TaskDal taskDal;
    
    public void setTaskDal(TaskDal taskDal)
    {
        this.taskDal = taskDal;
    }
    
    /***************** BLL 接口實現**************************************************/
    
    @Override
    public boolean createNewUser(User userData)
    {
        User user = userDal.getUserByEmail(userData.getEmail());
        if (user == null)
            user = userDal.getUserByMobile(userData.getMobile());

        if (user == null)
        {
            user = new User();
            user.setUserName(userData.getUserName());
            user.setPassword(userData.getPassword());
            user.setEmail(userData.getEmail());
            user.setMobile(userData.getMobile());
            // 1 表示新注册，未在线  2表示在线  3表示下线
            user.setStatus(1);
            this.userDal.saveUser(user);
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean login(User userData)
    {
        User user = userDal.getUserByEmailAndPwd(
                userData.getEmail(), userData.getPassword());
        if(user==null)
        {
            user = userDal.getUserByMobileAndPwd(
                    userData.getMobile(), userData.getPassword());
        }
        
        if(user == null)
            return false;

        return true;
    }
    
    

    @Override
    public User login(String emailOrMobile, String password)
    {
        User user = userDal.getUserByEmailAndPwd(
                emailOrMobile, password);
        if(user==null)
        {
            user = userDal.getUserByMobileAndPwd(
                    emailOrMobile, password);
        }
        
        //FIXME 如果是null，返回一个NullUser对象
        return user;

    }

    @Override
    public boolean logout(User userData)
    {
        User user = userDal.getUserByUserName(userData.getUserName());
        user.setStatus(1);
        userDal.saveUser(user);
        return true;
    }

    @Override
    public boolean changePassword(User userData, String newPassword)
    {
        User user = userDal.getUserByUserName(userData.getUserName());
        if(user.getPassword().equals(userData.getPassword()))
        {
            user.setPassword(newPassword);
            userDal.saveUser(user);
            return true;
        }
        return false;
    }

    @Override
    public User getUserByEmail(String email)
    {
        return userDal.getUserByEmail(email);
    }
    

    @Override
    public boolean updateUser(User userData)
    {
        if(userDal.getUserById(userData.getId())!=null)
        {
            userDal.saveUser(userData);
            return true;
        }
        
        return false;
        
    }
    
    @Override
    public List<Task> getMyTasks(User userData)
    {
        return taskDal.getTasksByUser(userData);
    }
    
    @Override
    public boolean createWorker(Worker worker)
    {
       userDal.saveWorker(worker);
       return true;
    }

    /*************************启动組件**********************************************/
    /**
     * 组件启动初期化
     */
    protected void activate(ComponentContext cc) throws Exception
    {
        log.info("{} is starting!", cc.getProperties().get("name") );
    }

}
